// pages/productlist/productlist.js
var app = getApp();
var GlobalData = require('../../utils/GlobalData.js');
var dataManager = require('../../utils/dataManager.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowDetails: false,
    productListArr: [],
    selArr: [true, false, false],
    productDetail: {},
    productDetailprice: { bonuspoints: 0, cash: 0 },
    payType: 0,
    payTypeArr: [],
    shoppingCarlen: 0,
    arrlength: 0,
    loading: false,
    selKindArr: [{ "img": "unsel", isclick: false }, { "img": "unsel", isclick: false }, { "img": "unsel", isclick: false }],
    kind: 5,
    isShowFiltrate: false,
    show: "meng",
    num: 1,

  },


  onHideFiltrate: function (e) {
    this.setData({
      isShowFiltrate: false,
    })
    let count = 0;
    /**判断是否选择 */
    for (let i = 0; i < this.data.selKindArr.length; i++) {
      if (this.data.selKindArr[i]["isclick"]) {
        count += 1;
        this.data.kind = i;
      }
    }
    // if (count == 0)
    //   this.data.kind = 3
    // if (this.data.kind >= 0)
    // this.onSelPayType(this.data.kind);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onComplete();



  },
  /**获取购物车回调函数 */
  onCompleteGetShoppingCar() {
    this.setData({
      shoppingCarlen: GlobalData.shopcarIntegral.length
    })
  },

  /**查看商品详情 */
  onProductDetails(item) {

    let id = item.currentTarget.dataset.id
    dataManager.rigisterObserver("getgoodsdetails", this.getGoodsDetails, this);
    dataManager.getProductDetails(id);
  },
  /**获取商品详情的回调函数 */
  getGoodsDetails() {
    dataManager.UseNavigateTo("/pages/productdetails/productdetails", false);
  },
  onClickCar(item) {
    let id = item.currentTarget.dataset.id
    dataManager.rigisterObserver("getgoodsdetails", this.getGoodsDetailsByClickCar, this);
    dataManager.getProductDetails(id);

  },
  /**点购物车获取商品信息后的回调函数 */
  getGoodsDetailsByClickCar() {
    console.log("item")
    this.data.payTypeArr = [];
    this.setData({
      show: "meng1",
      isShowDetails: !this.data.isShowDetails,
      productDetail: GlobalData.productDetails,
      productDetailprice: GlobalData.productDetails.exchanges[0],
    })
    for (let j = 0; j < this.data.productDetail.imgs.length; j++) {
      if (this.data.productDetail.imgs[j].sortno == 2) {
        this.data.productDetail['img'] = this.data.productDetail.imgs[j].path;
      }
    }
    for (let i = 0; i < this.data.productDetail.exchanges.length; i++) {
      if (this.data.productDetail.exchanges[i].cash == 0 && this.data.productDetail.exchanges[i].bonuspoints == 0)
        break;
      else
        this.data.payTypeArr.push(this.data.productDetail.exchanges[i])

    }
    this.setData({
      payTypeArr: this.data.payTypeArr,
      productDetail: this.data.productDetail,
    })
  },
  onClose() {
    this.setData({
      isShowDetails: false,
      payType: 0,
      show: "meng"
    })
    // this.isShowDetails = false;
    // this.payType = 0;
  },
  /**加减 */
  onChangeInfo(e) {
    let count = parseInt(e.currentTarget.dataset.id)
    if (count == 1) {
      this.data.num = this.data.num - 1 > 0 ? this.data.num - 1 : 0
    } else {
      this.data.num += 1;
    }
    this.setData({
      num: this.data.num,
    })
  },
  /**加入购物车 */
  onAddToCar() {
    // this.isShowDetails = false;
    let obj = {};
    obj["id"] = 0;
    obj["goodsid"] = this.data.productDetail.id;
    obj["bonuspoints"] = this.data.payTypeArr[this.data.payType].bonuspoints;
    obj["cash"] = this.data.payTypeArr[this.data.payType].cash;
    obj["num"] = this.data.num;
    dataManager.rigisterObserver("updateshoppingcart", this.onCompleteAddCar, this);
    dataManager.onAddCar(obj);

  },
  /**购物车添加成功的回调函数 */
  onCompleteAddCar() {
    var that = this;
    // app.onshowModal("添加购物车成功");

    wx.showModal({
      title: '提示',
      content: "添加购物车成功",
      showCancel: false,
      success: function (res) {
        that.setData({
          show: "meng"
        })
      }
    })

    dataManager.rigisterObserver("getshoppingcart", this.onCompleteGetShoppingCar, this);
    dataManager.getShoppingCar();
  },
  /**前往购物车 */
  onShoppingCard() {
    dataManager.UseNavigateTo("/pages/shoppingcar/shoppingcar", false)
  },
  /**选择支付方式 */
  onPayType(e) {
    let type = parseInt(e.currentTarget.dataset.type)
    this.setData({
      payType: type,
    })
    // this.payType = type;
    for (let i = 0; i < 3; i++) {
      if (i == this.data.payType) {
        this.data.selArr[i] = true;
      } else
        this.data.selArr[i] = false;
    }
    this.setData({
      selArr: this.data.selArr,
      productDetailprice: this.data.payTypeArr[this.data.payType],
    })
    // this.productDetailprice["bonuspoints"] = this.payTypeArr[this.payType].bonuspoints;
    // this.productDetailprice["cash"] = this.payTypeArr[this.payType].cash;
  },

  /**
 * 页面上拉触底事件的处理函数
 */
  /**触底加载更多信息 */
  onReachBottom: function () {
    if (!this.data.loading) {
      this.data.loading = true;
      let type = GlobalData.productType;
      dataManager.rigisterObserver("getMallProductList", this.onComplete, this);
      let kindId;
      let exchangeAddress;
      if (parseInt(type)) {
        if (parseInt(type) >= 2) {
          exchangeAddress = 2;
          kindId = type;
        } else {
          exchangeAddress = type;
          kindId = 0;
        }
      } else if (parseInt(type) == 0) {
        exchangeAddress = 0;
        kindId = 0;
      } else {
        exchangeAddress = 2;
        kindId = 0;
      }
      GlobalData.GoodStyle[GlobalData.producttypeStr] += 1;
      dataManager.getMallProductList(GlobalData.GoodStyle[GlobalData.producttypeStr], 10, true, exchangeAddress, kindId);
    }
  },
  /**触底加载完成的回调函数 */
  onComplete() {
    if (GlobalData.mallProductList[GlobalData.producttypeStr].length == this.data.arrlength)
      return;
    this.data.productListArr = GlobalData.clone(GlobalData.mallProductList[GlobalData.producttypeStr]);

    for (let i = 0; i < this.data.productListArr.length; i++) {
      this.data.productListArr[i]["price"] = this.data.productListArr[i].exchanges[0];
      for (let j = 0; j < this.data.productListArr[i].imgs.length; j++) {
        if (this.data.productListArr[i].imgs[j].sortno == 3) {
          this.data.productListArr[i]['img'] = this.data.productListArr[i].imgs[j].path;
        }
      }
    }
    this.setData({
      productListArr: this.data.productListArr
    });
    this.data.loading = false;
    this.data.arrlength = this.data.productListArr.length;
  },

  /**点击筛选 */
  onFiltrate() {
    this.setData({
      isShowFiltrate: !this.data.isShowFiltrate
    });
  },
  onSelKind(index) {
    this.data.kind = parseInt(index.currentTarget.dataset.id);
    let count = 0;
    for (let i = 0; i < this.data.selKindArr.length; i++) {
      if (i == this.data.kind) {
        this.data.selKindArr[i]["isclick"] = !this.data.selKindArr[i]["isclick"];
        if (this.data.selKindArr[i]["isclick"])
          this.data.selKindArr[i]["img"] = "sel";
        else
          this.data.selKindArr[i]["img"] = "unsel";

      } else {
        this.data.selKindArr[i]["isclick"] = false;
        this.data.selKindArr[i]["img"] = "unsel";
      }
    }
    if (!this.data.selKindArr[this.data.kind]["isclick"])
      this.data.kind = 3

    this.setData({
      selKindArr: this.data.selKindArr
    });
  },
  // 确认选择
  onComfireSel() {
    this.setData({
      isShowFiltrate: false,
    });
    if (!GlobalData.mallProductList[GlobalData.producttypeStr].length) return;
    this.onSelPayType(this.data.kind);

  },
  /**选择兑换方式 0 积分兑换，1 积分+现金 2 现金*/
  onSelPayType(type) {
    let arr = [];
    let globalProArr = GlobalData.clone(GlobalData.mallProductList[GlobalData.producttypeStr]);
    switch (type) {
      case 0:
        this.optionOfPayment(globalProArr, 0);
        break;
      case 1:
        this.optionOfPayment(globalProArr, 1);
        break;
      case 2:
        this.optionOfPayment(globalProArr, 2);
        break;
      case 3:
        this.optionOfPayment(globalProArr, 3);
   
        break;
    }
  },


  optionOfPayment(globalProArr, type) {
    let arr = [];
    for (let i = 0; i < globalProArr.length; i++) {
      for (let j = 0; j < globalProArr[i].exchanges.length; j++) {
        if (type == 0) {
          if (globalProArr[i].exchanges[j].cash == 0 && globalProArr[i].exchanges[j].bonuspoints != 0) {
            globalProArr[i]["price"] = globalProArr[i].exchanges[j];
            arr.push(globalProArr[i]);
            break;
          }
        } else if (type == 1) {
          if (globalProArr[i].exchanges[j].bonuspoints != 0 && globalProArr[i].exchanges[j].cash != 0) {
            globalProArr[i]["price"] = globalProArr[i].exchanges[j];
            arr.push(globalProArr[i]);
            break;
          }

        } else if (type == 2) {
          if (globalProArr[i].exchanges[j].bonuspoints == 0 && globalProArr[i].exchanges[j].cash != 0) {
            globalProArr[i]["price"] = globalProArr[i].exchanges[j];
            arr.push(globalProArr[i]);
            break;
          }
        } 
        else {
          globalProArr[i]["price"] = globalProArr[i].exchanges[0];
          arr.push(globalProArr[i]);
          break;
        }

      }
      for (let k = 0; k < globalProArr[i].imgs.length; k++) {
        if (globalProArr[i].imgs[k].sortno == 3) {
          globalProArr[i]['img'] = globalProArr[i].imgs[k].path;
        }
      }
    }
    this.setData({
      productListArr: arr,
    });
  },














  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    /**获取购物车信息 */
    dataManager.rigisterObserver("getshoppingcart", this.onCompleteGetShoppingCar, this);
    dataManager.getShoppingCar();
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})